/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * All Rights Researved.
 *
 * This library is developed and distributed for free usage,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * You can redistribute and/or modify it under the terms of the
 * Simplified BSD License
 *
 *
 *
 *
 *
 *
 *
 *
 * @Original Author: jin Chen
 * @date: Aug 27, 2003$
 * @version: 1.0
 */
package edu.psu.geovista.app.visualclassifier.classify.v2.colorpalette.v1;

/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * All Rights Researved.
 *
 * This library is developed and distributed for free usage,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * You can redistribute and/or modify it under the terms of the
 * Simplified BSD License
 *
 *
 *
 *
 *
 *
 *
 *
 * @Original Author: jin Chen
 * @date: Aug 27, 2003$
 * @version: 1.0
 */

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;

public class SwatchPanel extends JPanel {


    protected Color[] colors;
    protected Dimension swatchSize;
    protected Dimension numSwatches;
    protected Dimension gap;
    protected boolean outlineSquare;// make a outline in black color for the square

    public SwatchPanel() {
        initValues();
        initColors();
	setToolTipText(""); // register for events
	setOpaque(true);
	setBackground(Color.white);
	setRequestFocusEnabled(false);

        //setBorder(new javax.swing.border.TitledBorder("Select..."));
    }

   /* public boolean isFocusTraversable() {
        return false;
    }*/

    protected void initValues() {

    }

    public void paintComponent(Graphics g) {
         g.setColor(getBackground());
         g.fillRect(0,0,getWidth(), getHeight());
	 for (int row = 0; row < numSwatches.height; row++) {
	    for (int column = 0; column < numSwatches.width; column++) {

	      g.setColor( getColorForCell(column, row) );
		int x;
		if ((!this.getComponentOrientation().isLeftToRight()) &&
		    (this instanceof RecentSwatchPanel)) {
		    x = (numSwatches.width - column - 1) * (swatchSize.width + gap.width);
		} else {
		    x = column * (swatchSize.width + gap.width);
		}
		int y = row * (swatchSize.height + gap.height);
	        g.fillRect( x, y, swatchSize.width, swatchSize.height);


            if(this.outlineSquare ){
                g.setColor(Color.black );
                g.drawRect(x, y, swatchSize.width, swatchSize.height);
            }
		g.setColor(Color.black);
		//g.drawLine( x+swatchSize.width-1, y, x+swatchSize.width-1, y+swatchSize.height-1);
		//g.drawLine( x, y+swatchSize.height-1, x+swatchSize.width-1, y+swatchSize.height-1);
	    }
	 }
    }

    public Dimension getPreferredSize() {
        int x = numSwatches.width * (swatchSize.width + gap.width) -1;
	int y = numSwatches.height * (swatchSize.height + gap.height) -1;
        return new Dimension( x, y );
    }

    protected void initColors() {


    }

    public String getToolTipText(MouseEvent e) {
        Color color = getColorForLocation(e.getX(), e.getY());
        if (color!=null)
            return color.getRed()+", "+ color.getGreen() + ", " + color.getBlue();
        else
            return "no color";
    }

    public Color getColorForLocation( int x, int y ) {
        int column;
        if ((!this.getComponentOrientation().isLeftToRight()) &&
            (this instanceof edu.psu.geovista.app.visualclassifier.classify.v2.colorpalette.v1.RecentSwatchPanel)) {
            column = numSwatches.width - x / (swatchSize.width + gap.width) - 1;
        } else {
            column = x / (swatchSize.width + gap.width);
        }
        int row = y / (swatchSize.height + gap.height);
        if(column>=this.numSwatches.width ||row>=this.numSwatches.height ){
            System.out.println("out of range");
            return null;
        }
        Color c=null;
        try {
            c = getColorForCell(column, row);
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use Options | File Templates.
        }
        return c;
    }

    private Color getColorForCell( int column, int row) {
	    return colors[ (row * numSwatches.width) + column ]; // change data orientation here
    }




}

